package com.scala.zhenghui

import scala.collection.mutable.ArrayBuffer

/**
 * Created by Administrator on 2018/1/21/021.
 */
object YieldDemo {

  def main(args: Array[String]) {
    val a = Array(1, 2, 3, 4, 5)
    for (n <- a)
      print(n + ",")

//1
    println()
    val a2 = for (ele <- a) yield ele * ele
    for (n <- a2)
      print(n + ",")

    //2.
    println()
    val a3 = for (ele <- 0 until a.length if ele%2 ==0 ) yield ele * ele
    for (n <- a3)
      print(n + ",")

    //3.
    val b = new ArrayBuffer[Int]();
    b +=(1,2,3,4,5,6,7,8,9,10)
    for (n <- b)
      print(n + ",")
    //4.
    println()
    val b1 = for(ele <- 0 until b.length) yield  ele * ele
    for (n <- b1)
      print(n + ",")
 //5.函数式
 val af = Array(1, 2, 3, 4, 5)
    for (n <- af)
      print(n + ",")
    println()
   var af1 =   a.filter(_%2==0).map(_* 2)
    for (n <- af1)
      print(n + ",")

    println()
    var af2 =   a.filter{_%2==0}map{_* 2}
    for (n <- af2)
      print(n + ",")

  }





}
